﻿using H.Daos;
using HModel;

namespace H.Kernel.Utils
{
    /// <summary>
    /// 资源加载器
    /// </summary>
    public class ResLoader
    {
        private readonly IDao _Dao;


        public ResLoader(IDao dao)
        {
            _Dao = dao;
        }

        /// <summary>
        /// 加载一个资源信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ResEntity LoadRes(string type, string name)
        {
            ResEntity[] result = _Dao.Query<ResEntity>().Where("type", "=", type).Where("name", "=", name).Limit(1).Select();
            if (result.Length > 0)
            {
                return result[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 加载一个资源为图片
        /// </summary>
        /// <param name="type">图片的类型名</param>
        /// <param name="name">图片的二级名称</param>
        /// <returns>返回加载到的图片的内软锁</returns>
        public ImgEntity[] LoadImgRes(string type, string name)
        {
            ResEntity[] result = _Dao.Query<ResEntity>().Where("type", "=", type).Where("name", "=", name).Limit(1).Select();
            if (result.Length > 0 && !string.IsNullOrEmpty(result[0].ImgIds))
            {
                return _Dao.Query<ImgEntity>().Where("id", "in", result[0].ImgIds).Select();
            }
            else
            {
                return new ImgEntity[0];
            }
        }
    }
}
